package com.heima.wemedia;

import com.heima.file.service.FileStorageService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * @Description:
 * @Version: V1.0
 */
@SpringBootTest(classes=WemediaApplication.class)
@RunWith(SpringRunner.class)
public class OSSDemo {

    @Autowired
    FileStorageService fileStorageService;

    @Value("${file.oss.bucket-name}")
    String bucketName;

    @Value("${file.oss.endpoint}")
    String endpoint;

    @Value("${file.oss.web-site}")
    String webSite;

    @Value("${file.oss.profix}")
    String profix;


    @Test
    public void testUpload() throws Exception{
        // 上传Byte数组。
        FileInputStream inputStream = null;
        try {
            // 根据自己的操作系统修改文件所在的路径
            inputStream = new FileInputStream("/Users/Ares/Downloads/image/itheima.jpg");
            //https://heimaleadnewsoss.oss-cn-shanghai.aliyuncs.com/material/2021/1/20210112/a.jpg
            String fileId = fileStorageService.store(profix, "a.jpg", inputStream);
            System.out.println(webSite+fileId);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void testDelete() throws Exception {

        fileStorageService.delete("material/2021/1/20210112/a.jpg");
    }
}
